using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XWQControls
{
    public partial class XWQMessageBox : Form
    {
        private XWQMessageBoxResult result;

        public XWQMessageBox(Control control)
        {
            InitializeComponent();

            Text = "提示";
            FormBorderStyle = FormBorderStyle.FixedDialog;
            AutoScaleMode = AutoScaleMode.None;
            Size = new Size(341, 205);
            pictureBox_Icon.Size = new Size(50, 50);

            StartPosition = FormStartPosition.Manual;
            Left = control.Left + (+control.Width - Width) / 2;
            Top = control.Top + (control.Height - Height) / 2;
        }

        public XWQMessageBoxResult Show(string text, XWQMessageBoxIcon icon = XWQMessageBoxIcon.Information, XWQMessageBoxButtons button = XWQMessageBoxButtons.OK)
        {
            TopMost = true;
            int distance = 10;
            label_Text.Text = text;

            Bitmap bitmap = null;
            switch (icon)
            {
                case XWQMessageBoxIcon.Information:
                    bitmap = Properties.Resources.information;
                    break;
                case XWQMessageBoxIcon.Error:
                    bitmap = Properties.Resources.error;
                    break;
                case XWQMessageBoxIcon.Warning:
                    bitmap = Properties.Resources.warn;
                    break;
                case XWQMessageBoxIcon.Question:
                    bitmap = Properties.Resources.question;
                    break;
            }

            pictureBox_Icon.Image = bitmap;

            pictureBox_Icon.Left = (Width - pictureBox_Icon.Width - distance - label_Text.Width) / 2;
            label_Text.Left = pictureBox_Icon.Left + pictureBox_Icon.Width + distance;

            label_Text.Top = (pictureBox_Icon.Top + pictureBox_Icon.Height) / 2 + label_Text.Height / 4;

            List<Button> buttons = new();

            switch (button)
            {
                case XWQMessageBoxButtons.OK:
                    buttons.Add(new Button()
                    {
                        Text = "确定",
                        Tag = XWQMessageBoxResult.OK
                    });
                    break;
                case XWQMessageBoxButtons.OKCancel:
                    buttons.Add(new Button()
                    {
                        Text = "确定",
                        Tag = XWQMessageBoxResult.OK
                    });
                    buttons.Add(new Button()
                    {
                        Text = "取消",
                        Tag = XWQMessageBoxResult.Cancel
                    });
                    break;
                case XWQMessageBoxButtons.YesNo:
                    buttons.Add(new Button()
                    {
                        Text = "是",
                        Tag = XWQMessageBoxResult.Yes
                    });
                    buttons.Add(new Button()
                    {
                        Text = "否",
                        Tag = XWQMessageBoxResult.No
                    });
                    break;
                case XWQMessageBoxButtons.RetryCancel:
                    buttons.Add(new Button()
                    {
                        Text = "重试",
                        Tag = XWQMessageBoxResult.Retry
                    });
                    buttons.Add(new Button()
                    {
                        Text = "取消",
                        Tag = XWQMessageBoxResult.Cancel
                    });
                    break;
                case XWQMessageBoxButtons.AbortRetryIgnore:
                    buttons.Add(new Button()
                    {
                        Text = "终止",
                        Tag = XWQMessageBoxResult.Abort
                    });
                    buttons.Add(new Button()
                    {
                        Text = "重试",
                        Tag = XWQMessageBoxResult.Retry
                    });
                    buttons.Add(new Button()
                    {
                        Text = "忽略",
                        Tag = XWQMessageBoxResult.Ignore
                    });
                    break;
                case XWQMessageBoxButtons.YesNoCancel:
                    buttons.Add(new Button()
                    {
                        Text = "是",
                        Tag = XWQMessageBoxResult.Yes
                    });
                    buttons.Add(new Button()
                    {
                        Text = "否",
                        Tag = XWQMessageBoxResult.No
                    });
                    buttons.Add(new Button()
                    {
                        Text = "取消",
                        Tag = XWQMessageBoxResult.Cancel
                    });
                    break;
            }

            if (buttons.Count == 1)
            {
                buttons[0].Left = (panel.Width - buttons[0].Width) / 2;
            }
            else if (buttons.Count == 2)
            {
                buttons[0].Left = (panel.Width - distance) / 2 - buttons[0].Width;
                buttons[1].Left = (panel.Width - distance) / 2 + distance;
            }
            else
            {
                buttons[1].Left = (panel.Width - buttons[0].Width) / 2;
                buttons[0].Left = buttons[1].Left - distance - buttons[0].Width;
                buttons[2].Left = buttons[1].Left + buttons[1].Width + distance;
            }

            label_Text.BorderStyle = BorderStyle.None;

            buttons.ForEach(btn =>
            {
                btn.AutoSize = true;
                btn.Top = (panel.Height - btn.Height) / 2;
                btn.BackColor = Color.White;
                btn.Click += Btn_Click;
                panel.Controls.Add(btn);
            });

            ShowDialog();

            TopMost = false;
            return result;
        }

        private void Btn_Click(object sender, EventArgs e)
        {
            result = (XWQMessageBoxResult)((Button)sender).Tag;
            Hide();
        }

        private void XWQMessageBox_Load(object sender, EventArgs e)
        {

        }
    }

    //
    // 摘要:
    //     Specifies identifiers to indicate the return value of a dialog box.
    public enum XWQMessageBoxResult
    {
        //
        // 摘要:
        //     Nothing is returned from the dialog box. This means that the modal dialog continues
        //     running.
        None,
        //
        // 摘要:
        //     The dialog box return value is OK (usually sent from a button labeled OK).
        OK,
        //
        // 摘要:
        //     The dialog box return value is Cancel (usually sent from a button labeled Cancel).
        Cancel,
        //
        // 摘要:
        //     The dialog box return value is Abort (usually sent from a button labeled Abort).
        Abort,
        //
        // 摘要:
        //     The dialog box return value is Retry (usually sent from a button labeled Retry).
        Retry,
        //
        // 摘要:
        //     The dialog box return value is Ignore (usually sent from a button labeled Ignore).
        Ignore,
        //
        // 摘要:
        //     The dialog box return value is Yes (usually sent from a button labeled Yes).
        Yes,
        //
        // 摘要:
        //     The dialog box return value is No (usually sent from a button labeled No).
        No
    }

    //
    // 摘要:
    //     Specifies constants defining which buttons to display on a System.Windows.Forms.MessageBox.
    public enum XWQMessageBoxButtons
    {
        //
        // 摘要:
        //     The message box contains an OK button.
        OK,
        //
        // 摘要:
        //     The message box contains OK and Cancel buttons.
        OKCancel,
        //
        // 摘要:
        //     The message box contains Abort, Retry, and Ignore buttons.
        AbortRetryIgnore,
        //
        // 摘要:
        //     The message box contains Yes, No, and Cancel buttons.
        YesNoCancel,
        //
        // 摘要:
        //     The message box contains Yes and No buttons.
        YesNo,
        //
        // 摘要:
        //     The message box contains Retry and Cancel buttons.
        RetryCancel
    }

    public enum XWQMessageBoxIcon
    {
        //
        // 摘要:
        //     The message box contains no symbols.
        None = 0,
        //
        // 摘要:
        //     The message box contains a symbol consisting of a white X in a circle with a
        //     red background.
        Hand = 0x10,
        //
        // 摘要:
        //     The message box contains a symbol consisting of a question mark in a circle.
        //     The question mark message icon is no longer recommended because it does not clearly
        //     represent a specific type of message and because the phrasing of a message as
        //     a question could apply to any message type. In addition, users can confuse the
        //     question mark symbol with a help information symbol. Therefore, do not use this
        //     question mark symbol in your message boxes. The system continues to support its
        //     inclusion only for backward compatibility.
        Question = 0x20,
        //
        // 摘要:
        //     The message box contains a symbol consisting of an exclamation point in a triangle
        //     with a yellow background.
        Exclamation = 48,
        //
        // 摘要:
        //     The message box contains a symbol consisting of a lowercase letter i in a circle.
        Asterisk = 0x40,
        //
        // 摘要:
        //     The message box contains a symbol consisting of white X in a circle with a red
        //     background.
        Stop = 0x10,
        //
        // 摘要:
        //     The message box contains a symbol consisting of white X in a circle with a red
        //     background.
        Error = 0x10,
        //
        // 摘要:
        //     The message box contains a symbol consisting of an exclamation point in a triangle
        //     with a yellow background.
        Warning = 48,
        //
        // 摘要:
        //     The message box contains a symbol consisting of a lowercase letter i in a circle.
        Information = 0x40
    }
}
